<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/12/11  14:28
 */
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;

class Banner extends Common
{

    /*
	*	构造函数
	*/

    //BANNER列表
    public function lists(){

    
        $where=[];
        if(!empty(input('type'))){
            $where['type'] = input('type');

        }

        $banner = db('picture')->where($where)->order('sort desc')->select();


        $this->assign('banner',$banner);

        
        
        return $this->fetch();
    }


    public function edit()
    {
        $id = input("id");
        
        if($id){
            $data = db('picture')->where('id',$id)->find();
            $this->assign('da',$data);
        }
        
        return $this->fetch();
    }

    public function doEdit()
    {
            $data = input();
            $data['created_at'] = time();
         
            $file = request()->file('img');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public/static/uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['img_url'] = 'http://'.$_SERVER['HTTP_HOST'] . '/static/uploads/'.$info->getSaveName();
                    if(!empty($data['id'])){
                        $oldimg = db('picture')->where(['id'=>$data["id"]])->value('img_url');
                        $oldimgurl = ROOT_PATH . 'public/static/uploads/'.$oldimg;
                       /* if(file_exists($oldimgurl)){
                            unlink($oldimgurl);
                        }*/

                    }
                }else{
                    // 上传失败获取错误信息
                  $this->error($file->getError());
                }
            }    
            unset($data['img']);
            if($data['id']){
                //修改

                $res = db('picture')->where(['id'=>$data['id']])->update($data);
            }else{
                //添加

                $res = db('picture')->insert($data);
            }

            if($res)return json(['msg'=>'修改成功']);

            return json(['msg'=>'修改失败']);
    }

    public function delete()
    {
        $id = input('id');
        if(!$id)$this->error('参数有误');
        $data = db('picture')->where(['id'=>$id])->find();
        if(!$data)$this->error('数据不存在');
        $img = ROOT_PATH . 'public/static/uploads/'.$data['img_url'];
     /*   if($data['img_url'] || is_file($img)){
            unlink($img);
        }*/
        $res = db('picture')->where(['id'=>$id])->delete();
        if($res)return json(['msg'=>'修改成功']);

        return json(['msg'=>'修改失败']);
    }

//文字轮播
    public function typeface(){

        $banner = db('typeface')->select();
        $this->assign('banner',$banner);

        return $this->fetch();
    }
//添加
    public function edit_typeface(){
        $id = input("id");
        
        if($id){
            $data = db('typeface')->where('id',$id)->find();
            $this->assign('da',$data);
        }

        return $this->fetch();
    }
//
    public function doedit_typeface(){

        $input =input();

        if(!empty($input['id'])){

            $res = db('typeface')->update($input);
        }else{

            $res = db('typeface')->insert($input);
        }

        if($res)return json(['msg'=>'更新成功']);

        return json(['msg'=>'更新失败']);

    }

   public function delete_typeface()
    {
        $id = input('id');
        if(!$id)$this->error('参数有误');

        $res = db('typeface')->where(['id'=>$id])->delete();
        if($res)return json(['msg'=>'修改成功']);

        return json(['msg'=>'修改失败']);
    }

}